# 数列划分
#
# arr: 数列
# pivot: 基准值
# left: 划分后的子数列变量名（比基准值小的部分)
# right:  划分后的子数列变量名（比基准值大的部分)

function(partition arr pivot left right)
	foreach(x ${arr})
		if(${x} LESS ${pivot})
			list(APPEND _left ${x})
		else()
			list(APPEND _right ${x})
		endif()
	endforeach()
	set(${left} ${_left} PARENT_SCOPE)
	set(${right} ${_right} PARENT_SCOPE)
endfunction()


# 快速排序
#
# input: 输入数列
# res: 存放排序结果的数列变量名
function(quick_sort input res)
	list(LENGTH input input_len)
	if(${input_len} LESS_EQUAL 1)
		set(${res} "${input}" PARENT_SCOPE)
		return()
	endif()

	list(GET input 0 pivot)
	list(SUBLIST input 1 -1 input)

	partition("${input}" ${pivot} left right)
	quick_sort("${left}" left)
	quick_sort("${right}" right)
	list(APPEND _res ${left} ${pivot} ${right})
	set(${res} "${_res}" PARENT_SCOPE)
endfunction()

foreach(i RANGE 4 ${CMAKE_ARGC})
	list(APPEND input ${CMAKE_ARGV${i}})
endforeach()

message("排序前: ${input}")
quick_sort("${input}" res)
message("排序后: ${res}")

